package com.hxit.huaxiamelongarden.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hxit.huaxiamelongarden.domin.dto.IndexDto;
import com.hxit.huaxiamelongarden.domin.dto.LoginDto;
import com.hxit.huaxiamelongarden.domin.dto.RegisterDto;
import com.hxit.huaxiamelongarden.domin.dto.UserDto;
import com.hxit.huaxiamelongarden.domin.po.Content;
import com.hxit.huaxiamelongarden.domin.po.User;
import com.hxit.huaxiamelongarden.domin.vo.UserVo;
import com.hxit.huaxiamelongarden.mapper.ContentMapper;
import com.hxit.huaxiamelongarden.mapper.UserMapper;
import com.hxit.huaxiamelongarden.service.UserService;
import com.hxit.huaxiamelongarden.utils.NicknameGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName UserServiceImpl
 * @Author 12694
 * @Date 2025/1/20 15:49
 * @Version 1.0
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ContentMapper contentMapper;
    // 根据 LoginDto 查询用户
    @Override
    public User selectByLoginDto(LoginDto loginDto) {
        return userMapper.selectByLoginDto(loginDto);
    }

    /*添加用户*/
    @Override
    public int insert(RegisterDto registerDto) {
        if (userMapper.getUserByPhoneNumber(registerDto.getPhoneNumber()) != null) {
            return 0;
        }
        String nickname = NicknameGenerator.generateRandomUsername();
        User user = BeanUtil.copyProperties(registerDto, User.class);
        user.setNickname(nickname);
        return userMapper.insert(user);
    }

    //修改用户信息
    @Override
    public int updateById(UserDto userDto) {
        return userMapper.updateById(userDto);
    }

    //查询个人资料
    @Override
    public UserVo selectById(int userId) {
        User user = userMapper.selectById(userId);
        UserVo userVo = BeanUtil.copyProperties(user, UserVo.class);
        return userVo;
    }

    //首页模糊搜索用户，可根据昵称或电话号码或学号
    @Override
    public UserVo selectByLike(IndexDto indexDto) {
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        if (indexDto.getNickname() != null) {
            queryWrapper.like(User::getNickname,indexDto.getNickname());
        }
        if (indexDto.getPhoneNumber() != null) {
            queryWrapper.like(User::getPhoneNumber,indexDto.getPhoneNumber());
        }
        if (indexDto.getStudentId() != null) {
            queryWrapper.like(User::getStudentId,indexDto.getStudentId());
        }
        User user = userMapper.selectOne(queryWrapper);
        UserVo userVo = BeanUtil.copyProperties(user, UserVo.class);
        return userVo;
    }

    //根据contentId查询用户
    @Override
    public UserVo selectByContentId(int contentId) {
        Content content = contentMapper.selectById(contentId);
        User user = userMapper.selectById(content.getUserId());
        UserVo userVo = BeanUtil.copyProperties(user, UserVo.class);
        return userVo;
    }
}
